package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.fm.FragmentClubMemberFans;
import com.uchengwang.ui.cm_ui.me.fm.FragmentClubMemberFriends;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/7/5.
 * 社团添加成员时 只从粉丝中添加 还只是个人用户
 */

public class AddClubMemberActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private SlidingTabLayout tab;
    private MyViewPager pager;
    private String[] titles ;
    private ArrayList<Fragment> listFm = new ArrayList<>();

    private LinearLayout addMember_bottom;
    private AppCompatCheckBox addMember_box;
    private TextView addMember_all, addMember_agree;

    private int index =0;//当前页的角标
    private FragmentClubMemberFans fans;
    private FragmentClubMemberFriends friends;
    private List<UserBean> beanList;

    public static List<String> membersUids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_club_members);
        membersUids = getIntent().getStringArrayListExtra(BaseConfig.LIST);
        setTitle("添加成员");

        initView();
        initData();


    }

    private void initView() {
        tab = (SlidingTabLayout) findViewById(R.id.addMember_tab);
        pager = (MyViewPager) findViewById(R.id.addMember_pager);

        addMember_bottom = (LinearLayout) findViewById(R.id.addMember_bottom);
        addMember_box = (AppCompatCheckBox) findViewById(R.id.addMember_box);

        addMember_all = (TextView) findViewById(R.id.addMember_all);
        addMember_agree = (TextView) findViewById(R.id.addMember_agree);
        addMember_agree.setOnClickListener(this);

        tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                index = position;
            }
            @Override
            public void onTabReselect(int position) {
            }
        });

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                index = position;
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private void initData() {
        titles = new String[]{"粉丝", "好友"};

        fans = new FragmentClubMemberFans();
        friends = new FragmentClubMemberFriends();

        listFm.add(fans);
        listFm.add(friends);

        tab.setViewPager(pager, titles, this, listFm);

        addMember_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                switch (index){
                    case 0:
                        fans.ChooseAll(b);
                        break;
                    case 1:
                        friends.ChooseAll(b);
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (membersUids != null){
            membersUids = null;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.addMember_agree:
                //邀请加入
                switch (index){
                    case 0:
                        beanList = fans.getChoose();
                        break;
                    case 1:
                        beanList = friends.getChoose();
                        break;
                }

                if (beanList != null && beanList.size() >0){
                    final Long[] longs = new Long[beanList.size()];
                    for (int i=0; i<beanList.size(); i++){
                        longs[i] = beanList.get(i).getId();
                    }


                    LoadingUtils.getUtils().loading(this);
                    MapRequest map = new MapRequest();
                    map.put(BaseConfig.INVITED_UID, longs);
                    map.put(BaseConfig.INVITE_MSG, "邀请加入社团");
                    HttpManager.newInstance(this).query(ClubConfig.Invite.method, ClubConfig.Invite.URL_INVITE, map, new HttpCallBack() {
                        @Override
                        public void onError(int status, String msg) {
                            Log.d("jjf", "stutas="+status+"=msg="+msg);
                            UCommonUtil.getUtil().showToast(AddClubMemberActivity.this, "邀请失败");
                            LoadingUtils.getUtils().hide();
                        }

                        @Override
                        public void onResp(int status, String msg, String data) {
                            Log.d("jjf", "stutas="+status+"=msg="+msg);
                            LoadingUtils.getUtils().hide();

                            if (status != 0){
                                UCommonUtil.getUtil().showToast(AddClubMemberActivity.this, msg);
                                return;
                            }

                            UCommonUtil.getUtil().showToast(AddClubMemberActivity.this, "邀请成功");
//                            if (membersUids == null){
//                                membersUids = new ArrayList<String>();
//                            }
//                            List<String> strings = new ArrayList<String>();
//                            for (int i=0; i<longs.length; i++){
//                                strings.add(String.valueOf(longs[i]));
//                            }
//                            membersUids.addAll(strings);
//
//                            switch (index){
//                                case 0:
//                                    fans.notifyData();
//                                    break;
//                                case 1:
//                                    friends.notifyData();
//                                    break;
//                            }

                        }
                    });
                }else {
                    UCommonUtil.getUtil().showToast(this, "请选择");
                }


                break;
        }
    }
}
